package com.component;

import com.Listener.ActionDoneListener;
import com.bean.Farmer;
import com.bean.Product;
import com.bean.Users;
import com.dao.*;
import com.util.*;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class UpdateProductDialog extends JDialog {
    final int WIDTH = 500;
    final int HEIGHT = 400;

    private String ProductID;

    private JTextField IDField;
    private JTextField FarmerIDField;
    private JTextField typeField;
    private JTextField ProductNameField;
    private JTextField ProductPriceField;
    private JTextField pathField;
    private JTextArea descriptionArea;

    private ActionDoneListener listener;

    public UpdateProductDialog(JFrame jf, String title, boolean isModel, ActionDoneListener listener,String ProductID,int FarmerID){
        super(jf,title,isModel);
        this.listener  = listener;
        this.ProductID = ProductID;

        //组装视图
        this.setBounds((ScreenUtils.getScreenWidth()-WIDTH)/2,(ScreenUtils.getScreenHeight()-HEIGHT)/2,WIDTH,HEIGHT);

        Box vBox = Box.createVerticalBox();

        //组装农产品编号
        Box IDBox = Box.createHorizontalBox();
        JLabel IDLabel = new JLabel("农产品编号：");
        IDField = new JTextField(15);

        IDBox.add(IDLabel);
        IDBox.add(Box.createHorizontalStrut(15));
        IDBox.add(IDField);

        //组装农产品类型
        Box typeBox = Box.createHorizontalBox();
        JLabel typeLabel = new JLabel("农产品类型：");
        typeField = new JTextField(15);

        typeBox.add(typeLabel);
        typeBox.add(Box.createHorizontalStrut(15));
        typeBox.add(typeField);

        //组装农户编号
        Box FarmerIDBox = Box.createHorizontalBox();
        JLabel FarmerIDLabel = new JLabel("农 户 编 号 ：");
        FarmerIDField = new JTextField(15);

        FarmerIDBox.add(FarmerIDLabel);
        FarmerIDBox.add(Box.createHorizontalStrut(15));
        FarmerIDBox.add(FarmerIDField);


        //组装农产品名称
        Box ProductNameBox = Box.createHorizontalBox();
        JLabel ProductNameLabel = new JLabel("农产品名称：");
        ProductNameField = new JTextField(15);

        ProductNameBox.add(ProductNameLabel);
        ProductNameBox.add(Box.createHorizontalStrut(15));
        ProductNameBox.add(ProductNameField);

        //组装农产品价格
        Box ProductPriceBox = Box.createHorizontalBox();
        JLabel ProductPriceLabel = new JLabel("农产品价格：");
        ProductPriceField = new JTextField(15);

        ProductPriceBox.add(ProductPriceLabel);
        ProductPriceBox.add(Box.createHorizontalStrut(15));
        ProductPriceBox.add(ProductPriceField);


        //组装图片路径
        Box pathBox = Box.createHorizontalBox();
        JLabel pathLabel = new JLabel("图 片 路 径 ：");
        pathField = new JTextField(15);

        pathBox.add(pathLabel);
        pathBox.add(Box.createHorizontalStrut(15));
        pathBox.add(pathField);

        //组装描述信息
        Box descriptionBox = Box.createHorizontalBox();
        JLabel descriptionLabel = new JLabel("描 述 信 息 ：");
        descriptionArea = new JTextArea(3,15);

        descriptionBox.add(descriptionLabel);
        descriptionBox.add(Box.createHorizontalStrut(15));
        descriptionBox.add(descriptionArea);

        //组装按钮
        Box btnBox = Box.createHorizontalBox();
        JButton addBtn = new JButton("修改");
        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取用户的录入
                int ID = Integer.parseInt(IDField.getText().trim()); //添加的农产品编号
                int ProductCategoryID = Integer.parseInt(typeField.getText().trim());
                String ProductName = ProductNameField.getText().trim();
                Double Price = Double.valueOf(ProductPriceField.getText().trim());
                String ImagePath = pathField.getText().trim();
                String description = descriptionArea.getText().trim();
                if(IDField.getText().trim().length()!=5){
                    JOptionPane.showMessageDialog(jf, "农产品编号要为五位数字！");
                }
                else if(new SelectProduct().exitProductID(Integer.parseInt(ProductID))){
                    JOptionPane.showMessageDialog(jf,"已存在此农产品编号！");
                }
                else if(new SelectProductCategory().notexitProductCategoryID(ProductCategoryID)){
                    JOptionPane.showMessageDialog(jf, "不存在此农产品类型！");
                }
                else {
                    //将数据添加进数据库
                    Product product = new Product(ID,ProductCategoryID,FarmerID,ProductName,Price,ImagePath,description);
                    new UpdateProduct().updateProduct(product,Integer.parseInt(ProductID));
                    JOptionPane.showMessageDialog(jf, "修改成功!");
                    listener.done(null);
                    dispose(); // 关闭添加窗口
                }
            }
        });


        btnBox.add(addBtn);

        vBox.add(Box.createVerticalStrut(20));
        vBox.add(IDBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(typeBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(FarmerIDBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(ProductNameBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(ProductPriceBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(pathBox);
        vBox.add(Box.createVerticalStrut(10));
        vBox.add(descriptionBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(btnBox);

        //为了左右有间距，在vBox外层封装一个水平的Box，添加间隔
        Box hBox = Box.createHorizontalBox();
        hBox.add(Box.createHorizontalStrut(50));
        hBox.add(vBox);
        hBox.add(Box.createHorizontalStrut(50));

        requestData();

        this.add(hBox);
    }
    //请求数据
    public void requestData(){
        List<Product> list = new SelectProduct().getProductData();
        for(Product product:list)
        {
            if(Integer.toString(product.getProductID()).equals(ProductID)){
                IDField.setText(ProductID);
                typeField.setText(product.getProductCategoryID()+"");
                FarmerIDField.setText(product.getFarmerID()+"");
                ProductNameField.setText(product.getProductName());
                ProductPriceField.setText(product.getPrice()+"");
                pathField.setText(product.getImagePath());
                descriptionArea.setText(product.getDescription());
                break;
            }
        }
    }

}